<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="用户账号" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in userOptions"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productId">
        <el-select v-model="queryParams.productId" placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in productOptions"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="priceList" :default-expand-all="true">
      <el-table-column label="用户账号" align="center" prop="userName"/>
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="用户余额" align="center" prop="balance"/>
      <el-table-column label="产品售价" align="center" type="expand" width="300">
        <template slot-scope="props" gutter="2">
          <el-row type="flex" justify="end">
            <template v-for="(item, index) in props.row.productPriceVos">
              <el-col :span="6">
                <div class="grid-content bg-purple">
                  <p style="margin: 4px 0">
                    <a>{{ item.name }}（{{ item.isSystemPrice ? '默认价' : '定制价' }}）</a>
                    <input :ref="'p'+props.row.productId" v-model="item.price" style="width: 55px;">
                    <el-button size="mini" type="primary" @click="updatePrice(props.row.userId, item)">
                      修改
                    </el-button>
                  </p>
                </div>
              </el-col>
            </template>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
  import {userPrice, setPrice,productName} from "@/api/system/product";
  import {userName} from "@/api/system/user";

  export default {
    name: "Price",
    data() {
      return {
        userOptions: [],
        productOptions: [],
        // 遮罩层
        loading: true,

        // 总条数
        total: 0,
        // 定制价格表格数据
        priceList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,

          userId: null,
          productId: null
        }
      };
    },
    created() {
      this.getList();
      productName().then(response => {
        this.productOptions = response.data;
      });
      userName().then(response => {
        this.userOptions = response.data;
      });
    },
    methods: {
      updatePrice(userId, item) {
        setPrice({
          userId: userId,
          productId: item.id,
          price: item.price
        }).then(response => {
          if (response.code == 200) {
            this.$modal.msgSuccess('售价修改成功')
          } else {
            this.$modal.msgWarning(response.msg)
          }
          this.getList()
        })
      },
      /** 查询定制价格列表 */
      getList() {
        this.loading = true;
        userPrice(this.queryParams).then(response => {
          this.priceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          productId: null,
          price: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      }
    }
  };
</script>
<style scoped>

</style>
